/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAOs;
import entidad.Movie;
import java.util.ArrayList;
import javax.persistence.*;
import java.util.List;
import javax.persistence.Persistence;

/**
 *
 * @author felipe
 */
public class DaoMovie {
    
    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("GRLUPU");
    
    public void crear(Movie object){
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try{
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e){
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally{
            em.close();
        }
    }
    
    public void borrar(String name){
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Movie mov = em.find(Movie.class, name);
        if(mov != null){
            em.remove(mov);
        }
    }
    
    public Movie findMovie(String name){
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        return em.find(Movie.class,name);
    }

    
    
    public Movie leerNombre(Movie par){
        EntityManager em = emf.createEntityManager();
        Movie prov=null;
        Query q= em.createQuery("SELECT u from Movie u "+
                "WHERE u.nombre = :name" )
                .setParameter("name", par.getNombre());
        try {
            prov = (Movie) q.getSingleResult();
        } catch (NonUniqueResultException e) {
            prov = (Movie) q.getResultList().get(0);
            
        }
        catch(Exception e){
            e.printStackTrace();;            
        }
        finally {
            em.close();
            return prov;
        }
    }
    
    //Trae Todas las peliculas
    public List<Movie> AllMovies(){
        EntityManager em = emf.createEntityManager();
        List<Movie> cat = new ArrayList<Movie>();
        Query q= em.createQuery("SELECT u FROM Movie u");
        try {
            List<Movie> tempCat = q.getResultList();
        for(Movie c: tempCat){
            Movie prod = new Movie();
            prod.setNombre(c.getNombre());
            prod.setCover(c.getCover());
            prod.setActores(c.getActores());
            prod.setAnno(c.getAnno());
            prod.setAvailableForPurchase(c.getAvailableForPurchase());
            prod.setAvailableForRental(c.getAvailableForRental());
            prod.setDuracion(c.getDuracion());
            prod.setPalabrasClave(c.getPalabrasClave());
            prod.setRating(c.getRating());
            prod.setDirector(c.getDirector());
            

            cat.add(prod);
        }
        }
        catch(Exception e){
            e.printStackTrace();;            
        }
        finally {
            em.close();
            return cat;
        }
    }
    
    //Trae las peliculas segun el genero
    public List<Movie> MoviesByGenero(String palabra){
        EntityManager em = emf.createEntityManager();
        List<Movie> cat = new ArrayList<Movie>();
        Query q= em.createQuery("SELECT u FROM Movie u "+
                "WHERE u.genero = :gen")
                .setParameter("gen", palabra);
        try {
            List<Movie> tempCat = q.getResultList();
        for(Movie c: tempCat){
            Movie prod = new Movie();
            prod.setNombre(c.getNombre());
            prod.setCover(c.getCover());
            prod.setActores(c.getActores());
            prod.setAnno(c.getAnno());
            prod.setAvailableForPurchase(c.getAvailableForPurchase());
            prod.setAvailableForRental(c.getAvailableForRental());
            prod.setDuracion(c.getDuracion());
            prod.setPalabrasClave(c.getPalabrasClave());
            prod.setRating(c.getRating());
            prod.setDirector(c.getDirector());
            

            cat.add(prod);
        }
        }
        catch(Exception e){
            e.printStackTrace();;            
        }
        finally {
            em.close();
            return cat;
        }
    }
}
